package com.cis467.mrball;

import com.cis467.mrball.backend.GameManagement;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public class LevelSelect extends Activity implements OnTouchListener{

	/** A handle to the View in which the game is running. */
	private LevelSelectView mLevelSelectView;
	
	private String mMode;

	/**
	 * Invoked during init to give the Activity a chance to set up its Menu.
	 * 
	 * @param menu the Menu to which entries may be added
	 * @return true
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		return true;
	}

	/**
	 * Invoked when the user selects an item from the Menu.
	 * 
	 * @param item the Menu entry which was selected
	 * @return true if the Menu item was legit (and we consumed it), false
	 *         otherwise
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return true;
	}
	
	
	/**
	 * Invoked when the Activity is created.
	 * 
	 * @param savedInstanceState a Bundle containing state saved from a previous
	 *        execution, or null if this is a new execution
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// tell system to use the layout defined in our XML file
		setContentView(R.layout.levelselect_layout);

		// get handles to view
		mLevelSelectView = (LevelSelectView) findViewById(R.id.levelselect);

		mMode = getIntent().getStringExtra("mode");
		
		mLevelSelectView.setMode(mMode);
		
		mLevelSelectView.requestFocus();

		mLevelSelectView.setFocusableInTouchMode(true);
		mLevelSelectView.setOnTouchListener(this);  
	}

	/**
	 * Notification that something is about to happen, to give the Activity a
	 * chance to save state.
	 * 
	 * @param outState a Bundle into which this Activity should save its state
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// just have the View's thread save its state into our Bundle
		super.onSaveInstanceState(outState);
	}

	public boolean onTouch(View v, MotionEvent event) {

		int action = event.getAction();

		if(action == MotionEvent.ACTION_DOWN) {
			
			if(mLevelSelectView.handleTouch((int) event.getX(), (int) event.getY())) {
				
				if(mMode.equals("Play")) {
					
					Intent i = new Intent(v.getContext(), MrBall.class);
					
					i.putExtra("mode", "Single");
					
					i.putExtra("levelXML", mLevelSelectView.getLevel().toString());
					
					startActivity(i);
				}
				else if(mMode.equals("Load")) {
					
					Intent i = new Intent(v.getContext(), BuildLevel.class);
				
					i.putExtra("levelXML", mLevelSelectView.getLevel().toString());
					
					setResult(RESULT_OK, i);
					
					finish();
				}
				
				else if(mMode.equals("Save")) {
					
					Toast.makeText(this, "Level Saved", Toast.LENGTH_SHORT).show();
					
					try {
						//GameManagement.save_Level(mLevelSelectView.getLevel(), false);
					}
					catch (Exception e) {
						
					}
				}
			}
		}

		return true;
	}
}